/// <reference path="../../sage/sage.util.js" />

var Sky2Painter = Base.extend({
    initialize: function initialize() {
        this.SKY_BLUE = [141, 208, 235];
        this.SKY_LIGHT_BLUE = [166, 214, 231];
        this.NIGHT_LIGHT_GRAY = [45, 0, 102];
        this.NIGHT_DARK_GRAY = [34, 0, 85];
        this.SECTION_COUNT = 30.0; //out of 360 degrees of sky!
        this.SECTION_ANGLE = util._360 / this.SECTION_COUNT;
        this.SUN_LOCATION = util._180;
    },
    morphColor: function morphColor(c1, c2, pct) {
        var c = [0, 0, 0];
        for (var i = 0; i < 3; i++)
            c[i] = c1[i] + Math.round((c2[i] - c1[i]) * pct);
        return c;
    },
    paint: function paint(context) {
        var viewWidth = context.viewPort.size.w,
            viewAngle = context.viewPort.horizontalViewableAngle,
			skyHeight = Math.round(context.viewPort.size.h / 2.0),
            totalSkyWidth = viewWidth * (util._360) / viewAngle,
            sectionWidth = totalSkyWidth / this.SECTION_COUNT,
            nibbleAngle = context.camera.lineOfSight,
            skyColor1 = null, 
            skyColor2 = null,
            pixelOffset = 0,
            isAlternateColor = true,
            now = new Date();

        var sunHeightPct = .5;
        var minutesPct = now.getMinutes() / 60.0;
        var hourOfDay = minutesPct + now.getHours();
        
        //var seconds = now.getTime() / 1000.0;
        //var testNum = 24.0 * ((seconds % 20.0) / 20.0);
        //var hourOfDay = testNum;
        //var minutesPct = testNum - parseInt(hourOfDay);

        var c1, c2, cs;

        // SUN COLOR AND LOCATION
        if ((hourOfDay > 7) && (hourOfDay <= 13.0)) { 
            this.SUN_LOCATION = 0;
            cs = [255, 255, 50];
            sunHeightPct = 1.0 - ((hourOfDay - 7.0) / (13.0 - 7.0));
        }
        else if ((hourOfDay > 13) && (hourOfDay <= 19.0)) { 
            this.SUN_LOCATION = util._180;
            cs = [255, 255, 50];
            sunHeightPct = (hourOfDay - 13.0) / (19.0 - 13.0);
        }
        else if ((hourOfDay > 19) || (hourOfDay <= 1.0)) { 
            this.SUN_LOCATION = 0;
            cs = [255, 255, 255];
            sunHeightPct = 1.0 - ( ((hourOfDay <= 1.0 ? (hourOfDay + 24) : hourOfDay) - 19.0) / (25.0 - 19.0) );
        }
        else if ((hourOfDay > 1) && (hourOfDay <= 7.0)) { 
            this.SUN_LOCATION = util._180;
            cs = [255, 255, 255];
            sunHeightPct = (hourOfDay - 1.0) / (7.0 - 1.0);
        }

        // SKY COLOR AND LOCATION
        if ((hourOfDay <= 7.0) || (hourOfDay >= 20.0)) {
            // night
            c1 = this.NIGHT_LIGHT_GRAY;
            c2 = this.NIGHT_DARK_GRAY;
        }
        else if ((hourOfDay >= 8.0) && (hourOfDay <= 19.0)) {
            // day
            c1 = this.SKY_LIGHT_BLUE;
            c2 = this.SKY_BLUE;
        }
        else if ((hourOfDay > 7.0) && (hourOfDay < 8.0)) {
            // sunrise
            c1 = this.morphColor(this.NIGHT_LIGHT_GRAY, this.SKY_LIGHT_BLUE, minutesPct);
            c2 = this.morphColor(this.NIGHT_DARK_GRAY, this.SKY_BLUE, minutesPct);
        }
        else { //((hourOfDay > 19.0) && (hourOfDay < 20.0))
            // sunset
            c1 = this.morphColor(this.SKY_LIGHT_BLUE, this.NIGHT_LIGHT_GRAY, minutesPct);
            c2 = this.morphColor(this.SKY_BLUE, this.NIGHT_DARK_GRAY, minutesPct);
        }
        skyColor1 = "rgb(" + c1[0] + ", " + c1[1] + ", " + c1[2] + ")";
        skyColor2 = "rgb(" + c2[0] + ", " + c2[1] + ", " + c2[2] + ")";

        while (nibbleAngle < 0) nibbleAngle += util._360;
        isAlternateColor = Math.floor(nibbleAngle / this.SECTION_ANGLE) % 2;

        if (nibbleAngle >= this.SECTION_ANGLE) nibbleAngle = nibbleAngle % this.SECTION_ANGLE;
        offset = Math.round((nibbleAngle * sectionWidth) / this.SECTION_ANGLE);

        context.backLayer.fillStyle = isAlternateColor ? skyColor2 : skyColor1;
        context.backLayer.fillRect(0, 0, viewWidth, skyHeight);

        context.backLayer.fillStyle = isAlternateColor ? skyColor1 : skyColor2;
        for (var x = offset; x <= viewWidth; x += (sectionWidth * 2)) {
            var w = sectionWidth;
            if ((w + x) > viewWidth) w -= x - viewWidth;
            if (w > 0)
                context.backLayer.fillRect(x, 0, w, skyHeight);
        }

        // Sample sun thingy?
        var minView = (context.camera.lineOfSight - viewAngle / 2.0) - util.radians(5), // added degrees as a hack to make sure the entire sun shows up
            maxView = (context.camera.lineOfSight + viewAngle / 2.0) + util.radians(5);

        if (minView > util._360) while (minView > util._360) minView -= util._360;
        else while (minView <= 0) minView += util._360;
        if (maxView > util._360) while (maxView > util._360) maxView -= util._360;
        else while (maxView <= 0) maxView += util._360;

        var showSun = false;
        if (minView < maxView) {
            showSun = (this.SUN_LOCATION >= minView && this.SUN_LOCATION <= maxView);
        }
        else {
            showSun = (this.SUN_LOCATION >= minView || this.SUN_LOCATION <= maxView);
        }

        if (showSun) {
            var ssize = skyHeight / 8, halo = ssize * 0.1;
            var soff = this.SUN_LOCATION - ((minView < maxView) ? minView : (minView - util._360));
            var sloc = viewWidth - (soff * viewWidth / viewAngle);

            for (var i = 0; i < 4; i++) {
                context.backLayer.fillStyle = "rgba(" + cs[0] + ", " + cs[1] + ", " + cs[2] + ", " + (0.1 + (i * 0.2)) + ")"; cs
                context.backLayer.beginPath();
                context.backLayer.arc(sloc, skyHeight * sunHeightPct, ssize - (i * halo), 0, util._360);
                context.backLayer.closePath();
                context.backLayer.fill();
            }
        }
    }
});
